package com.wuyb.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuyb.pojo.ChatFilter;
import com.wuyb.pojo.entity.BaseResponse;
import com.wuyb.pojo.param.ChatFilterParam;
import com.wuyb.service.AdminLogService;
import com.wuyb.service.ChatFilterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 管理聊天词过滤
 *
 * @Author: wyb
 * @Date: 2024/01/08/18:56
 * @Description:
 */
@Slf4j
@Api(tags = "管理聊天过滤词")
@RestController
@RequestMapping("/chatFilter")
public class ChatFilterController {

    @Resource
    private ChatFilterService chatFilterService;

    @Resource
    private AdminLogService adminLogService;

    @PreAuthorize("@SGExpressionRoot.hasAuthority('chatFilter')")
    @ApiOperation("分页查看过滤词列表")
    @GetMapping("/{current}/{size}")
    public BaseResponse<?> select(@PathVariable Integer current, @PathVariable Integer size) {
        log.info(String.format("get:/chatFilter 分页查看过滤词列表。%d , %d", current, size));
        BaseResponse<Page<ChatFilter>> select = chatFilterService.select(current, size);
        adminLogService.insert("chatFilter", "查看", "/chatFilter", select.getCode() == 200);
        return select;
    }

    @PreAuthorize("@SGExpressionRoot.hasAuthority('chatFilter')")
    @ApiOperation("添加过滤词")
    @PostMapping()
    public BaseResponse<?> insert(@RequestBody @Validated ChatFilter chatFilter) {
        log.info(String.format("post:/chatFilter 添加过滤词。%s", chatFilter));
        BaseResponse<?> insert = chatFilterService.insert(chatFilter);
        ChatFilterParam chatFilterParam = new ChatFilterParam(chatFilter.getId(), chatFilter.getFilterContent(), chatFilter.getFilterRule(), chatFilter.getReplaceContent());
        adminLogService.insert("chatFilter", "新增", "/chatFilter", JSONObject.toJSONString(chatFilterParam), insert.getCode() == 200);
        return insert;
    }

    @PreAuthorize("@SGExpressionRoot.hasAuthority('chatFilter')")
    @ApiOperation("修改过滤词")
    @PutMapping("/{id}")
    public BaseResponse<?> update(@RequestBody @Validated ChatFilter chatFilter, @PathVariable Integer id) {
        chatFilter.setId(id);
        log.info(String.format("put:/chatFilter 修改过滤词。%s", chatFilter));
        BaseResponse<?> baseResponse = chatFilterService.updateOne(chatFilter);
        ChatFilterParam chatFilterParam = new ChatFilterParam(chatFilter.getId(), chatFilter.getFilterContent(), chatFilter.getFilterRule(), chatFilter.getReplaceContent());
        adminLogService.insert("chatFilter", "编辑", String.format("/chatFilter/%s", id), JSONObject.toJSONString(chatFilterParam), baseResponse.getCode() == 200);
        return baseResponse;
    }

    @PreAuthorize("@SGExpressionRoot.hasAuthority('chatFilter')")
    @ApiOperation("删除过滤词")
    @DeleteMapping("/{id}")
    public BaseResponse<?> delete(@PathVariable Integer id) {
        log.info(String.format("delete:/chatFilter 删除过滤词。%s", id));
        BaseResponse<?> delete = chatFilterService.delete(id);

        adminLogService.insert("chatFilter", "删除", String.format("/chatFilter/%s", id), delete.getCode() == 200);
        return delete;
    }

    @PreAuthorize("@SGExpressionRoot.hasAuthority('chatFilter')")
    @ApiOperation("设置过滤词是否启用")
    @PutMapping("/{id}/enable/{enable}")
    public BaseResponse<?> setAdmin(@PathVariable Integer id, @PathVariable Integer enable) {
        log.info(String.format("Put:/chatFilter/{id}/enable/{enable} 设置过滤词是否启用。%d,%d", id, enable));
        BaseResponse<?> baseResponse = chatFilterService.setEnable(id, enable == 1);
        adminLogService.insert("chatFilter", "编辑", String.format("/chatFilter/%s/enable/%s", id, enable), baseResponse.getCode() == 200);
        return baseResponse;
    }
}
